package cn.com.czpb.client.app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.http.SslError;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.utils.LogUtils;

/**
 * Created by 猪猪侠 on 2017/11/16.
 */

public class HtmlActivity extends MToolBarActivity {
    @BindView(R.id.webview_id)
    WebView webView;

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_html;
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        WebSettings webSettings = webView.getSettings();
        String ua = webSettings.getUserAgentString();
        LogUtils.i("ua-->"+ua);
        webSettings.setUserAgentString(ua+"; gxrbapp/");
        String ua2 = webSettings.getUserAgentString();
        LogUtils.i("ua-->"+ua2);
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
//        webView.loadUrl("startAppSettings");
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {

                // *** NEVER DO THIS!!! ***
                // super.onReceivedSslError(view, handler, error);

                // let's ignore ssl error
                handler.proceed();
            }
        });
        webView.setWebChromeClient(new MyWebChromeClient());
    }
    public class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
            final JsResult finalJsResult = jsResult;
            new AlertDialog.Builder(view.getContext()).setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finalJsResult.confirm();
                }
            }).setCancelable(false).create().show();
            return true;
        }
    }

    @Override
    public void finish() {
        super.finish();
        webView.loadUrl("about:blank");
    }
}
